package com.taiji.dicom4cloud.smart.config;

import com.alibaba.fastjson.JSONObject;
import com.taiji.dicom4cloud.common.core.utils.OkHttpUtils;

/**
 * @ClassName CameraApi
 * @Description
 * @Author HX
 * @Date 2021/9/18 15:24
 **/
public class CameraApi {

    private static final String BASE_URL = "https://www.cloud-dahua.com";

    /**
     * 获取认证token
     * @param clientId
     * @param clientSecret
     * @return
     */
    public static String getToken(String clientId,String clientSecret) {

        String jsonStr = OkHttpUtils.builder()
                .url(BASE_URL+"/gateway/auth/oauth/token")
                .addParam("grant_type","client_credentials")
                .addParam("scope","server")
                .addParam("client_id",clientId)
                .addParam("client_secret",clientSecret)
                .addHeader("Content-Type","x-www-form-urlencoded")
                .post(false)
                .sync();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        return jsonObject.getString("access_token");
    }

    /**
     * 获取单个设备信息
     * @param deviceId
     * @param token
     * @return
     */
    public static JSONObject  getDeviceInfo(String deviceId,String token){
        String jsonStr = OkHttpUtils.builder()
                .url(BASE_URL+"/gateway/device/api/deviceInfo")
                .addParam("deviceId",deviceId)
                .addHeader("Content-Type","application/json; charset=utf-8")
                .addHeader("Authorization","Bearer "+token)
                .post(true)
                .sync();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        return jsonObject;
    }

    /**
     * 设备抓图
     * @param deviceId
     * @param channelId
     * @param token
     * @return
     */
    public static JSONObject  getChannelSnapPic(String deviceId,String channelId,String token){
        String jsonStr = OkHttpUtils.builder()
                .url(BASE_URL+"/gateway/device/api/getChannelSnapPic")
                .addParam("deviceId",deviceId)
                .addParam("channelId",channelId)
                .addHeader("Content-Type","application/json; charset=utf-8")
                .addHeader("Authorization","Bearer "+token)
                .post(true)
                .sync();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        return jsonObject;
    }

    /**
     * 根据时间段查询本地录像片段
     * @param deviceId
     * @param channelId
     * @param beginTime
     * @param endTime
     * @param token
     * @return
     */
    public static JSONObject getVideoLocalRecords(String deviceId,String channelId,String beginTime,String endTime, String token){
        String jsonStr = OkHttpUtils.builder()
                .url(BASE_URL+"/gateway/device/api/getVideoLocalRecords")
                .addParam("pageNum","1")
                .addParam("pageSize","100")
                .addParam("deviceId",deviceId)
                .addParam("channelId",channelId)
                .addParam("beginTime",beginTime)
                .addParam("endTime",endTime)
                .addHeader("Content-Type","application/json; charset=utf-8")
                .addHeader("Authorization","Bearer "+token)
                .post(true)
                .sync();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        return jsonObject;
    }

    /**
     * 根据设备录像片段生成hls直播地址
     * @param deviceId
     * @param channelId
     * @param beginTime
     * @param endTime
     * @param token
     * @return
     */
    public static JSONObject createDeviceRecordHls(String deviceId,String channelId,String beginTime,String endTime, String token){
        String jsonStr = OkHttpUtils.builder()
                .url(BASE_URL+"/gateway/device/api/createDeviceRecordHls")
                .addParam("recordType","localRecord")//录像类型, "localRecord":本地录像，"cloudRecord":云录像
                .addParam("deviceId",deviceId)
                .addParam("channelId",channelId)
                .addParam("beginTime",beginTime)//"yyyy-MM-dd HH:mm:ss"
                .addParam("endTime",endTime)//"yyyy-MM-dd HH:mm:ss"
                .addHeader("Content-Type","application/json; charset=utf-8")
                .addHeader("Authorization","Bearer "+token)
                .post(true)
                .sync();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        return jsonObject;
    }
}
